﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using YArchitech.LIB;

namespace HYElectricSystemSetting
{
	public partial class CreateItem : System.Windows.Forms.Form
	{
		public CreateItem(ComboBox cmb, LineType lt)
		{
			this.InitializeComponent();
			this.cmbBaseOn.Items.Clear();
			this.ltp = lt;
			if (this.ltp == LineType.WireType)
			{
				if (ElectricSystemSettingData.WireTypeDatas == null)
				{
					return;
				}
				using (List<WireTypeData>.Enumerator enumerator = ElectricSystemSettingData.WireTypeDatas.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						WireTypeData item = enumerator.Current;
						this.cmbBaseOn.Items.Add(item);
						this.cmbBaseOn.DisplayMember = "WireType";
					}
					goto IL_EB;
				}
			}
			if (this.ltp == LineType.ConduitType)
			{
				if (ElectricSystemSettingData.ConduitTypeDatas == null)
				{
					return;
				}
				this.cmbBaseOn.DisplayMember = "ConduitTypeSet";
				foreach (ConduitTypeSetClass item2 in ElectricSystemSettingData.ConduitTypeDatas)
				{
					this.cmbBaseOn.Items.Add(item2);
				}
			}
			IL_EB:
			if (this.cmbBaseOn.Items.Count > 0)
			{
				this.cmbBaseOn.SelectedIndex = 0;
			}
			this.cmbk = cmb;
		}

		private void btnAddWireType_Click(object sender, EventArgs e)
		{
			int num = 1;
			int num2 = 0;
			this.ischeck = true;
			if (this.textBox1.Text.Trim() == "")
			{
				MessageBox.Show("名称不能为空！");
				this.ischeck = false;
				return;
			}
			if (this.ltp == LineType.WireType)
			{
				foreach (WireTypeData wireTypeData in ElectricSystemSettingData.WireTypeDatas)
				{
					int num3 = Convert.ToInt32(wireTypeData.Id);
					if (num3 >= num)
					{
						num = num3;
						num++;
					}
					if (wireTypeData.WireType == this.textBox1.Text)
					{
						MessageBox.Show("名称已存在！");
						this.ischeck = false;
						return;
					}
				}
				WireTypeData item = new WireTypeData(num.ToString(), this.textBox1.Text, this.cmbBaseOn.SelectedItem.ToString());
				ElectricSystemSettingData.WireTypeDatas.Add(item);
				this.cmbk.Items.Add(item);
				WireTypeData wireTypeData2 = (WireTypeData)this.cmbBaseOn.SelectedItem;
				ElectricSystemSettingData.WireDataClone(num.ToString(), wireTypeData2.Id);
				for (int i = 0; i < this.cmbk.Items.Count; i++)
				{
					WireTypeData wireTypeData3 = (WireTypeData)this.cmbk.Items[i];
					if (this.textBox1.Text == wireTypeData3.WireType)
					{
						this.cmbk.SelectedIndex = i;
					}
				}
			}
			else if (this.ltp == LineType.ConduitType)
			{
				foreach (ConduitTypeSetClass conduitTypeSetClass in ElectricSystemSettingData.ConduitTypeDatas)
				{
					int num4 = Convert.ToInt32(conduitTypeSetClass.Id);
					if (num4 >= num2)
					{
						num2 = num4;
						num2++;
					}
					if (conduitTypeSetClass.ConduitTypeSet == this.textBox1.Text)
					{
						MessageBox.Show("名称已存在！");
						this.ischeck = false;
						return;
					}
				}
				ConduitTypeSetClass item2 = new ConduitTypeSetClass(num2.ToString(), this.textBox1.Text, this.cmbBaseOn.SelectedItem.ToString());
				ElectricSystemSettingData.ConduitTypeDatas.Add(item2);
				this.cmbk.Items.Add(item2);
				ConduitTypeSetClass conduitTypeSetClass2 = (ConduitTypeSetClass)this.cmbBaseOn.SelectedItem;
				ElectricSystemSettingData.PipeClone(num2.ToString(), conduitTypeSetClass2.Id);
				for (int j = 0; j < this.cmbk.Items.Count; j++)
				{
					ConduitTypeSetClass conduitTypeSetClass3 = (ConduitTypeSetClass)this.cmbk.Items[j];
					if (this.textBox1.Text == conduitTypeSetClass3.ConduitTypeSet)
					{
						this.cmbk.SelectedIndex = j;
					}
				}
			}
			base.Close();
		}

		private void btnCancle_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void CreateItem_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (base.DialogResult == DialogResult.Yes && !this.ischeck)
			{
				e.Cancel = true;
			}
		}

		private void CreateItem_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
		}

		private ComboBox cmbk;

		private LineType ltp;

		private bool ischeck = true;
	}
}
